Merge Sort Algorithm

The Merge Sort Algorithm is an efficient, widely-used sorting algorithm that employs a divide-and-conquer strategy to sort a given list of elements. The basic idea behind the algorithm is to repeatedly split the list into two equal halves until each sub-list contains only one element, and then merge these smaller sub-lists back together in a sorted manner. This process of dividing the list and merging the sorted sub-lists back together is performed recursively, resulting in a sorted list. Merge sort is highly efficient, with a time complexity of O(n log n), making it a popular choice for various applications in computer science and data processing. The algorithm involves two major steps: dividing the given list and merging the sorted sub-lists. In the dividing step, the list is split into two equal halves (or almost equal, if the list has an odd number of elements). This is done recursively until each sub-list contains only one element. In the merging step, the sub-lists are combined together by comparing the first elements of each sub-list and placing the smaller element in the new sorted list. This process is repeated until all elements from the sub-lists have been compared and placed in the sorted list. The merging process is also performed recursively, building up larger and larger sorted sub-lists until the entire list is sorted. This elegant combination of dividing and merging results in a highly efficient and powerful sorting algorithm.
/*
 Petar 'PetarV' Velickovic
 Algorithm: Merge Sort
*/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
#define MAX_N 1000001
using namespace std;
typedef long long lld;

int n;
int niz[MAX_N], tmp[MAX_N];

//Merge sort algoritam za sortiranje niza
//Slozenost: O(n log n)

inline void merge(int left, int mid, int right)
{
    int h,i,j,k;
    h = left;
    i = left;
    j = mid+1;
    while (h <= mid && j <= right)
    {
        if (niz[h] <= niz[j])
        {
            tmp[i] = niz[h];
            h++;
        }
        else
        {
            tmp[i] = niz[j];
            j++;
        }
        i++;
    }
    if (h > mid)
    {
        for(k=j;k<=right;k++)
        {
            tmp[i] = niz[k];
            i++;
        }    
    }
    else
    {
        for(k=h;k<=mid;k++)
        {
            tmp[i] = niz[k];
            i++;
        }
    }
    for(k=left;k<=right;k++) niz[k] = tmp[k];
}

void mergeSort(int left, int right)
{
    if (left == right) return;
    int MID = (left+right)/2;
    mergeSort(left, MID);
    mergeSort(MID+1, right);
    merge(left, MID, right);
}

int main()
{
    n = 5;
    niz[0] = 4;
    niz[1] = 2;
    niz[2] = 5;
    niz[3] = 1;
    niz[4] = 3;
    mergeSort(0, n-1);
    for (int i=0;i<n;i++) printf("%d ",niz[i]);
    printf("\n");
    return 0;
}

LANGUAGE:

DARK MODE: